package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.common.validator.ValidatorUtils;
import com.diankangwo.common.validator.group.UpdateGroup;
import com.diankangwo.factory.annotation.LoginUser2;
import com.diankangwo.factory.dao.TdDiscussMapper;
import com.diankangwo.factory.dao.TdMessageMapper;
import com.diankangwo.factory.dao.TdUserinfoMapper;
import com.diankangwo.factory.entity.TdCommodity;
import com.diankangwo.factory.entity.TdDiscuss;
import com.diankangwo.factory.entity.TdMessage;
import com.diankangwo.factory.entity.TdUserinfo;
import com.diankangwo.factory.service.TdCommodityService;
import com.diankangwo.sys.controller.AbstractController;
import com.diankangwo.sys.entity.SysUserEntity;
import com.diankangwo.util.MessageType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 评论controller
 * Created by wuye on 2017/9/8.
 */
@RestController
@RequestMapping("/fac/discuss")
public class TdDiscussController extends AbstractController {

    @Autowired
    private TdDiscussMapper tdDiscussMapper;
    @Autowired
    private TdCommodityService tdCommodityService;
    @Autowired
    private TdMessageMapper tdMessageMapper;
    @Autowired
    private TdUserinfoMapper tdUserinfoMapper;


    /**
     * 获取评论列表
     */
    @ApiOperation(value = "根据条件所有评论列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @RequestMapping("/list")
    @RequiresPermissions("fac:discuss:list")
    public R list(@LoginUser2 SysUserEntity sysUserEntity, @RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdDiscuss> tdDiscusses = new ArrayList<>();
        List<TdDiscuss> tdDiscusses2 = tdDiscussMapper.queryList(query);
        if (sysUserEntity.getTdFactoryid() != -1) {
            for (int i = 0; i < tdDiscusses2.size(); i++) {
                TdCommodity tdCommodity = tdCommodityService.selectByPrimaryKey(tdDiscusses2.get(i).getCommodityid());
                if (Integer.parseInt(tdCommodity.getFactoryid()) == sysUserEntity.getTdFactoryid()) {
                    tdDiscusses.add(tdDiscusses2.get(i));
                }
            }
        } else {
            tdDiscusses.addAll(tdDiscusses2);
        }
        int total = tdDiscussMapper.queryTotal(params);
        PageUtils pageUtil = new PageUtils(tdDiscusses, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 修改评论
     */
    @ApiOperation(value = "修改评论", notes = "")
    @ApiImplicitParam(name = "discuss", value = "修改评论", required = true, dataType = "TdDiscuss")
    @SysLog("修改评论")
    @RequestMapping("/update")
    @RequiresPermissions("fac:discuss:update")
    public R update(@RequestBody TdDiscuss tdDiscuss) {
        ValidatorUtils.validateEntity(tdDiscuss, UpdateGroup.class);
        tdDiscussMapper.updateByPrimaryKey(tdDiscuss);
        return R.ok();
    }

    /**
     * 删除评论
     */
    @ApiOperation(value = "删除评论", notes = "")
    @ApiImplicitParam(name = "discussIds", value = "评论ID", required = true, dataType = "int")
    @SysLog("删除评论")
    @RequestMapping("/delete")
    @RequiresPermissions("fac:discuss:delete")
    public R delete(int discussIds) {
        tdDiscussMapper.deleteByPrimaryKey(discussIds);
        List<TdDiscuss> tdDiscussList = tdDiscussMapper.queryByreplyId(discussIds);
        for (int i = 0; i < tdDiscussList.size(); i++) {
            tdDiscussMapper.deleteByPrimaryKey(tdDiscussList.get(i).getId());
        }
        return R.ok();
    }

    /**
     * 获取单个评论详情
     */
    @ApiOperation(value = "获取单个评论详情", notes = "")
    @ApiImplicitParam(name = "discussIds", value = "评论ID", required = true, dataType = "int")
    @SysLog("获取单个评论详情")
    @RequestMapping("/ById")
    @RequiresPermissions("fac:discuss:ById")
    public R ById(int discussIds) {
        TdDiscuss tdDiscuss = tdDiscussMapper.selectByPrimaryKey(discussIds);
        return R.ok().put("tdDiscussList", tdDiscuss);
    }

    /**
     * 加载评论回复列表
     */
    @ApiOperation(value = "加载评论回复列表", notes = "")
    @ApiImplicitParam(name = "discussIds", value = "评论ID", required = true, dataType = "int")
    @SysLog("加载评论回复列表")
    @RequestMapping("/reply")
    @RequiresPermissions("fac:discuss:reply")
    public R reply(int discussIds) {
        List<TdDiscuss> tdDiscussList = tdDiscussMapper.queryByreplyId(discussIds);
        return R.ok().put("tdDiscussList", tdDiscussList);
    }

    /**
     * 回复评论
     */
    @ApiOperation(value = "回复评论", notes = "")
    @ApiImplicitParam(name = "discussIds", value = "评论ID", required = true, dataType = "int")
    @SysLog("回复评论")
    @RequestMapping("/adminreply")
    @RequiresPermissions("fac:discuss:adminreply")
    public R adminreply(int discussIds, String content) {
        TdDiscuss tdDiscuss = tdDiscussMapper.selectByPrimaryKey(discussIds);
        TdUserinfo tdUserinfo = tdUserinfoMapper.selectById(tdDiscuss.getUserid());
        TdMessage tdMessage = new TdMessage("管理员对你的评论做出了回复：" + content, 0, new Date(), MessageType.order.getName(), tdUserinfo.getUid(), "");
        tdMessageMapper.insert(tdMessage);
        return R.ok();
    }
}
